/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.inf.ufg.model;

import br.inf.ufg.factory.ConexaoFactoryMySql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author umarley.faria
 */
public class EventosDAO {
    
    
    
    private Connection con;
    private Statement stmt;
    private Statement stmtNavegar;
    private ResultSet rsNavegar;
    
    
    
    public EventosDAO() throws ClassNotFoundException, SQLException{        
         
             con = ConexaoFactoryMySql.getConnection();
             stmt = con.createStatement();
             stmtNavegar = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
                     ResultSet.CONCUR_UPDATABLE );
             rsNavegar = stmtNavegar.executeQuery( "select * from eventos" );        
    
    }
    
    
    public boolean salvarRegistro( Eventos evento ) throws SQLException, Exception{
          
         // int cod = evento.getIdEvento();
          String nome = evento.getNome(); 
          String data = evento.getData();
          String hora = evento.getHora();
          int isAtivo = evento.getIsAtivo();
          int isAssentoReservado = evento.getIsAssentoReservado();
          int local = evento.getLocal();
           
        
        try {
            stmt.executeUpdate( "INSERT INTO `svi`.`eventos` (`NOME`, `DATA`, "
                                 + "`HORA`, `IS_ATIVO`, `LOCAL`, "
                                 + "`IS_ASSENTO_RESERVADO`) "
                                 + " VALUES ('"+nome+"', "
                                 + "'"+data+"', '"+hora+"', "+isAtivo+", "+local+", "+isAssentoReservado+");");
            rsNavegar = stmtNavegar.executeQuery( "select * from eventos" );
            return true;
            
        } catch ( SQLException e) {
                 throw new Exception("Erro ao inserir dados. Tente novamente " + e.getMessage());                      
        }
          
      }
    
    public int getUltimoEventoInserido() throws SQLException {
    

          ResultSet rs = stmt.executeQuery("SELECT IDEVENTO FROM eventos E " +
                                           "ORDER BY IDEVENTO DESC " +
                                           "LIMIT 1");
           if ( rs.next() ) {
                  return rs.getInt( "IDEVENTO" );
                                 
              } else {
                  return 0;
           }
        
    } 
    
    public boolean isEventoExiste(int cod) throws SQLException {
    

          ResultSet rs = stmt.executeQuery("SELECT COUNT(IDEVENTO) QTD FROM "
                  + "eventos E WHERE E.IDEVENTO = " + cod);
          
          int qtd = rs.getInt("QTD");
          
          if(qtd == 1){
              return true;
          }else{
              return false;
          }
        
    }
    
    
    
    
    
    
    
}
